/*
 * ChangeDeltaPanel.java
 *
 * Created on 17 April 2003, 10:00
 * Copyright (C) 2003, Velocityme Partnership
 */

package com.velocityme.client.gui;

import java.rmi.RemoteException;
import com.velocityme.interfaces.NodePK;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.util.Collection;
import java.util.Iterator;
import javax.swing.text.ChangedCharSetException;
import javax.swing.text.Document;
import javax.swing.text.html.HTMLDocument;
import javax.swing.text.html.HTMLEditorKit;
import javax.xml.transform.Result;
import javax.xml.transform.Source;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.stream.StreamResult;
import javax.xml.transform.stream.StreamSource;
import com.velocityme.client.gui.exception.ExceptionWindow;
import com.velocityme.client.utility.StatelessRemoteClientSessionUtil;
import com.velocityme.interfaces.DBObjectPK;
import com.velocityme.interfaces.KeySession;

/**
 *
 * @author  Robert Crida Work
 */
public class ChangeDeltaPanel extends javax.swing.JPanel {
    
    KeySession m_key;
    NodePK m_nodePK;
    
    /** Creates new form ChangeDeltaPanel */
    public ChangeDeltaPanel(KeySession p_key, NodePK p_nodePK) {
        initComponents();
        m_key = p_key;
        m_nodePK = p_nodePK;
    }
    
    void refresh() {
        try {
            String text = StatelessRemoteClientSessionUtil.getSession().getDBObjectChangeLogXML(m_key, new DBObjectPK(m_nodePK.getNodeId()));
            Transformer transformer = TransformerFactory.newInstance().newTransformer(new StreamSource("changeLog.xsl"));
            ByteArrayInputStream inputStream = new ByteArrayInputStream(text.getBytes());
            Source source = new StreamSource(inputStream);
            ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
            Result output = new StreamResult(outputStream);
            transformer.transform(source, output);
            jEditorPaneLog.setEditable(false);
            jEditorPaneLog.setContentType("text/html");
            HTMLDocument doc = (HTMLDocument)((HTMLEditorKit)jEditorPaneLog.getEditorKit()).createDefaultDocument();
            doc.putProperty("IgnoreCharsetDirective", new Boolean(true));
            jEditorPaneLog.read(new ByteArrayInputStream(outputStream.toByteArray()), doc);
        }
        catch(Exception e) {
            ExceptionWindow.showError(e);
        }
    }
    
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    private void initComponents() {//GEN-BEGIN:initComponents
        jScrollPane1 = new javax.swing.JScrollPane();
        jEditorPaneLog = new javax.swing.JEditorPane();

        setLayout(new java.awt.BorderLayout());

        addComponentListener(new java.awt.event.ComponentAdapter() {
            public void componentShown(java.awt.event.ComponentEvent evt) {
                formComponentShown(evt);
            }
        });

        jScrollPane1.setPreferredSize(new java.awt.Dimension(453, 200));
        jScrollPane1.setViewportView(jEditorPaneLog);

        add(jScrollPane1, java.awt.BorderLayout.CENTER);

    }//GEN-END:initComponents

    private void formComponentShown(java.awt.event.ComponentEvent evt) {//GEN-FIRST:event_formComponentShown
        // Add your handling code here:
        refresh();
    }//GEN-LAST:event_formComponentShown
    
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JEditorPane jEditorPaneLog;
    private javax.swing.JScrollPane jScrollPane1;
    // End of variables declaration//GEN-END:variables
    
}
